const http = require('http')
const fs = require('fs')
const url= require('url')
const querystring = require('querystring')
const mysql = require('mysql')
const zlib = require('zlib')

let db = mysql.createPool({host:'localhost',user:'root',password:'root',port:3307,database:'blue_learn'});

const server = http.createServer((req,res)=>{
    //接收get数据
    let {pathname,query} = url.parse(req.url,true)
    res.setHeader("Content-Type","text/html; charset=UTF-8");

    switch (pathname) {
        case '/reg': //处理注册
            let {name,pass} = query;
            //表单数据验证
            //...
            db.query('select * from blue_user where name = "'+name+'"',(err,dbdata)=>{
                if(err){
                    res.writeHeader(501);
                    res.write(err.message);
                    res.end();
                }else{
                    if(dbdata.length>0){
                        res.writeHeader(403);
                        res.write("已存在此账号");
                        res.end();
                    }else{
                        db.query("Insert into blue_user (name,pass) VALUES ('"+name+"','"+pass+"')",(err,idbdata)=>{
                            if(err){
                                res.writeHeader(501);
                                res.write(err.message);
                                res.end();
                            }else{
                                console.log(idbdata);
                                res.write("创建成功");
                                res.end();
                            }
                        })
                    }
                }
            })
            break;
        case '/login': //处理登录-类似注册
            res.write("login");
            res.end();
            break;
        default: //静态文件
            fs.stat(`www${req.url}`,(err,stat)=>{
                if(err){
                    res.writeHeader(404);
                    res.write("Not Found");
                    res.end();
                }else{
                    if(req.headers['if-modified-since']){
                        let c_time = new Date(req.headers['if-modified-since']);
                        let c_time_stamp = Math.floor(c_time.getTime()/1000);
                        let s_time_stamp = Math.floor(stat.mtime.getTime()/1000);
                        if(s_time_stamp>c_time_stamp){
                            toSendFile();
                        }else{
                            //不发送，使用浏览器本地缓存
                            res.writeHeader(304);
                            res.write("Not Modified");
                            res.end();
                        }
                    }else{
                        toSendFile();
                    }
                    
                }
                //发送源文件
                function toSendFile(){
                    let rs = fs.createReadStream(`www${req.url}`);
                    rs.on('error',err=>{
                        res.writeHead(404)
                        res.write('Not Found')
                        res.end()
                    })
                    res.setHeader("Content-Encoding","gzip")
                    res.setHeader("Last-Modified",stat.mtime.toUTCString())
                    let gz = zlib.createGzip()
                    rs.pipe(gz).pipe(res)
                }
            })
            break;
    }
})
server.listen(8080)
